package Persistance::SystemCenterDao;

use strict;
use warnings;
use 5.010;

use Utils::DbUtils qw(query_for_array update update_by_map count);
use Utils::Constants;

use Pojo::User;
use Pojo::Option;
use Pojo::Privilege;

use Exporter qw(import);

sub get_all_users {
	return query_for_array(Utils::Constants::GET_ALL_USERS_SQL, "Pojo::User");
}

sub get_user_by_id {
    my $user_id = shift;

    return query_for_array(Utils::Constants::GET_USER_BY_ID_SQL, "Pojo::User", $user_id);
}

sub update_user_by_id {
    my ($id, $user_fields_map) = @_;
    my $table_name = Utils::Constants::USER_TABLE_NAME;

    return update_by_map($table_name, $id, $user_fields_map);
}

sub delete_user_by_id {
    my ($id) = @_;
    my $result_code = update(Utils::Constants::DELETE_USER_SQL, $id);

    return $result_code;
}

sub update_user_password {
    my ($id, $current_pwd, $new_pwd) = @_;
    my $affected_row = update(Utils::Constants::UPDATE_USER_PASSWORD_SQL, $new_pwd, $id, $current_pwd);

    return $affected_row;
}

sub reset_user_password_to_default {
    my ($id, $default) = @_;

    return update(Utils::Constants::RESET_USER_PASSWORD_TO_DEFAULT_SQL, ,$default, $id);
}

sub update_option_by_user_id {
    my ($user_id, $option_key, $option_value) = @_;
    my $affected_row = update(Utils::Constants::INSERT_OR_UPDATE_BY_USERID_AND_KEY, $option_key, $option_value, $user_id);

    return $affected_row;
}

sub get_options_by_user_id {
    my $user_id = shift;

    return query_for_array(Utils::Constants::GET_ALL_OPTIONS_BY_USERID, "Pojo::Option", $user_id);
}

sub count_user_by_name_and_password {
    my ($name, $password) = @_;
    my $count = count(Utils::Constants::CHECK_USER_EXISTANCE_SQL, $name, $password);

    return $count;
}

sub get_user_by_name_and_password {
    my ($name, $password) = @_;

    return query_for_array(Utils::Constants::GET_USER_BY_NAME_AND_PASSWORD, "Pojo::User", $name, $password);
}

sub add_new_user {
    my ($name, $password, $role) = @_;
    my $result_code = update(Utils::Constants::ADD_NEW_USER_SQL, $name, $role, $password);

    return $result_code;
}

sub get_all_privileges {
    return query_for_array(Utils::Constants::GET_ALL_PRIVILEGES, "Pojo::Privilege");
}

1;
